/**
 * [simpleJudgmentType 判断数据类型（简化值）]
 * @param  {[type]} data [数据]
 * @return {[type]}      [简化值]
 */
export function simpleJudgmentType(data) {
	const type = Object.prototype.toString.call(data);
	switch (type) {
		case '[object Object]':
		return 'obj';
		case '[object Array]':
		return 'arr';
		case '[object String]':
		return 'str';
		case '[object Number]':
		return 'num';
		case '[object Null]':
		return 'null';
		case '[object Boolean]':
		return 'bo';
		case '[object Function]':
		return 'fun';
		case '[object Undefined]':
		return 'un';
		default:
		return type;
	}
}
/**
 * [objectSubAttributeAssign 更新对象属性值（obj，arr类型不一致不更新）]
 * @param  {[type]} original [原对象]
 * @param  {[type]} update   [新对象]
 * @return {[type]}          [description]
 */
export function objectSubAttributeAssign(original, update) {
	if (simpleJudgmentType(original) === 'obj' && simpleJudgmentType(update) === 'obj') {
		var key, val1, val2;
		for (key in update) {
			val1 = original[key];
			val2 = update[key];
			if (simpleJudgmentType(val1) === 'obj') {
				if (simpleJudgmentType(val2) === 'obj') {
					objectSubAttributeAssign(val1, val2);
				}
				continue;
			} else if (simpleJudgmentType(val1) === 'arr') {
				if (simpleJudgmentType(val1) !== simpleJudgmentType(val2)) {
					continue;
				}
			}
			original[key] = update[key];
		}
		return original;
	} else {
		throw new Error('入参数据必选为对象！');
	}
}
/**
 * [frontToAfterTransformation 前台数据转后台数据属性组合与拆分（objectSubAttributeAssign进阶版）]
 * @param  {[type]} original [原对象]
 * @param  {[type]} update   [新对象]
 * @param  {[type]} oMapping [映射对象]
 * @return {[type]}          [description]
 */
export function frontToAfterTransformation(original, update, oMapping, self) {
	if (!oMapping) {
		throw new Error('frontToAfterTransformation 映射对象不存在！ 如没有请使用 objectSubAttributeAssign');
	}
	var _oMapping = {};
	objectSubAttributeAssign(_oMapping, oMapping);
	const isReverse = _oMapping.isReverse;
	delete _oMapping.isReverse;
	var oMappingKey, temporaryArr, isAdd;
	if (simpleJudgmentType(original) === 'obj' && simpleJudgmentType(update) === 'obj') {
		var key, val1, val2;
		for (key in update) {
			val1 = original[key];
			val2 = update[key];
			if (simpleJudgmentType(val2) === 'obj') {
				if (simpleJudgmentType(val1) === 'obj') {
					frontToAfterTransformation(val1, val2, oMapping, self);
				} else if (simpleJudgmentType(val1) === 'un') {
					if (self) {
						self.$set(original, key, {});
					} else {
						original[key] = {};
					}
					frontToAfterTransformation(original[key], val2, oMapping, self);
				}
				continue;
			}
			for (oMappingKey in _oMapping) {
				var arrI;
				temporaryArr = _oMapping[oMappingKey].split('&');
				if (!isReverse) {
					if (key === _oMapping[oMappingKey]) {
						for (arrI = 0; arrI < temporaryArr.length; arrI++) {
							original[temporaryArr[arrI]] = update[key][arrI];
						}
						break;
					}
				} else {
					if (key === oMappingKey) {
						original[_oMapping[oMappingKey]] = [];
						for (arrI = 0; arrI < temporaryArr.length; arrI++) {
							original[_oMapping[oMappingKey]].push(
								update[temporaryArr[arrI]]
							);
						}
						break;
					}
				}
			}
			if (simpleJudgmentType(val2) === 'obj') {
				val1 = {};
				frontToAfterTransformation(val1, val2, oMapping, self);
				continue;
			} else if (simpleJudgmentType(val2) === 'arr') {
				if (self) {
					self.$set(original, key, []);
				} else {
					original[key] = [];
				}
				for (var i = 0; i < val2.length; i++) {
					if (simpleJudgmentType(val2[i]) === 'obj') {
						if (self) {
							self.$set(original[key], i, {});
						} else {
							original[key][i] = {};
						}
						frontToAfterTransformation(original[key][i], val2[i], oMapping, self);
					}
				}
				continue;
			}
			// 20220113z 过滤被规则转化的数据不再被原属性覆盖
			if (!isReverse) {
				for (oMappingKey in _oMapping) {
					if (_oMapping[oMappingKey].indexOf(key) !== -1) {
						isAdd = true;
						break;
					}
				}
			}
			if (!isAdd) {
				if (self) {
					self.$set(original, key, update[key]);
				} else {
					original[key] = update[key];
				}
			}
		}
		return original;
	} else {
		throw new Error('入参数据必选为对象！');
	}
}
// /**
//  * [deepClone 深度拷贝（拷贝的方法引用地址）]
//  * @param  {[type]} val [拷贝的数据源]
//  * @return {[type]}     [拷贝后的数据]
//  */
// export function deepClone(val) {
// 	// 判断如果不是引用类型，直接返回数据即可
// 	if (typeof val === 'string' || typeof val === 'number' || typeof val === 'boolean' || typeof val === 'undefined') {
// 	  return val;
// 	} else if (Array.isArray(val)) { // 如果是数组，则定义一个新数组，完成复制后返回
// 	  // 注意，这里判断数组不能用typeof，因为typeof Array 返回的是object
// 	  // console.log(typeof [])  // --> object
// 	  var _arr = [];
// 	  val.forEach(item => { _arr.push(item); });
// 	  return _arr;
// 	} else if (typeof val === 'object') {
// 	  var _obj = {};
// 	  for (const key in val) {
// 	      _obj[key] = deepClone(val[key]);
// 	  }
// 	  return _obj;
// 	} else {
// 		return val;
// 	}
// }

export function recursionTreeDisabled(treeData) {
	for (var i = 0; i < treeData.length; i++) {
		treeData[i].disabled = treeData[i].isEnable === 0;
		// treeData[i].selectable = treeData[i].isEnable === 1;
		if (treeData[i].children) {
			recursionTreeDisabled(treeData[i].children);
		};
	};
    return treeData;
}
/**
 * [supplementZero 补充位数]
 * @param  {[type]}   val   [数据]
 * @param  {[Number]} digit [生成位数]
 * @return {[type]}         [description]
 */
export function supplementZero(val, digit) {
	if (String(val).length > digit) return val;
	return (Array(digit).join(0) + val).slice(-digit);
}
